<template>
    <!-- 使用ref来获取dom元素 -->
    <div class="list" ref="wrapper">
        <div>
            <div class="area">
                <div class="title">Present City</div>
                <div class="buttom-list">
                    <div class="buttom-list-wrapper">
                        <div class="buttom">{{presentCity}}</div>
                    </div>     
                </div>
            </div>
            <div class="area">
                <div class="title">Popular City</div>
                <div class="buttom-list">
                    <div class="buttom-list-wrapper" 
                    v-for="item of hot" 
                    :key="item.id"
                    
                    >
                        <div class="buttom" 
                         @click="handleClickCity(item.name)"
                        >
                        {{item.name}}
                        </div>
                    </div>
                </div>
            </div>
            <!-- 下面的ref是否加冒号差别很大 -->
            <div class="area"
             v-for="(item, key) of cities"
              :key="key"
              :ref="key"
            >
                <div class="title">{{key}}</div>
                <div class="item-list">
                    <div class="item" 
                    v-for="innerItem of item" 
                    :key="innerItem.id"
                    @click="handleClickCity(innerItem.name)"
                    >{{innerItem.name}}</div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
// Better-scroll需要先导入
import Bscroll from 'better-scroll'
import { mapState,mapMutations } from 'vuex'
export default {
    name:"CityList",
    props: {
        hot: Array,
        cities: Object,
        letter: String
    },
    computed: {
        ...mapState({
            presentCity: 'city'
        })
    },
    methods: {
        handleClickCity (city) {
            // 没有用上actions，所以下面一行代码注释掉
            // this.$store.dispatch('clickChangeCity', city)
            // .commit才能调用mutations里的方法
            // this.$store.commit('changeCity',city)
            this.changeCity(city)
            this.$router.push('/')
        },
        ...mapMutations(['changeCity'])
    },
    mounted () {
        // 以下是Bsroll的用法
        this.scroll = new Bscroll(this.$refs.wrapper,{click:true})
    },
    watch: {
        letter () {
            // 可以打印出$refs查看结构以便获取数据
            const element = this.$refs[this.letter][0]
            // console.log(this.$refs[this.letter][0])
            this.scroll.scrollToElement(element)
        }
    }
}
</script>
<style lang="stylus" scoped>
@import "~styles/varibles.styl"
    // 一像素边框问题总是不生效，索性注释掉
    // .border-topbuttom
    //     &:before
    //         border-color: #ccc
    //     &:after
    //         border-color: #ccc
    .list
        overflow: hidden
        position: absolute 
        top: 1.58rem
        bottom: 0
        left: 0
        right: 0
        .title
            line-height: .54rem
            background: #ccc
        .buttom-list
            // 直觉告诉我这个作用不是隐藏溢出那么简单。。
            overflow: hidden
            padding: .1rem .6rem .1rem .1rem
            // 用wrapper包裹住内容是个好习惯
            .buttom-list-wrapper
                float: left
                width: 33.33%
                .buttom
                    margin: .1rem
                    padding: .1rem 
                    border: .02rem solid #ccc
                    border-radius: .06rem   
                    text-align: center
        .item-list
            .item
                line-height: .54rem
                padding-left: .2rem
</style>